import os
import sys
import configparser


class AppConfigParser(object):
    """封装configparser"""

    def __init__(self, conf):
        this_dir = sys.path[0]
        if conf == "test":
            config_path = os.path.join(this_dir, 'conf/test.ini')
        elif conf == "develop":
            config_path = os.path.join(this_dir, 'conf/develop.ini')
        elif conf == "production":
            config_path = os.path.join(this_dir, 'conf/production.ini')
        self.cfg = configparser.ConfigParser()
        self.cfg.read(config_path)
        super(AppConfigParser, self).__init__()

    def get(self, key, value):
        # 获取键值
        return self.cfg[key][value]

    def get_postgres_url(self):
        # 获取postgresurl
        PG_HOST = self.get("postgres", "host")
        PG_PORT = self.get("postgres", "port")
        PG_USERNAME = self.get("postgres", "username")
        PG_PASSWORD = self.get("postgres", "password")
        PG_DATABASE = self.get("postgres", "database")
        PG_URI = 'postgresql+psycopg2://{}:{}@{}:{}/{}'.format(
            PG_USERNAME, PG_PASSWORD, PG_HOST, PG_PORT, PG_DATABASE)
        return PG_URI

    def get_redis_info(self):
        # 获取redis
        REDIS_HOST = self.get("redis", "host")
        REDIS_PORT = self.get("redis", "port")
        REDIS_DB = self.get("redis", "db")
        return dict(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)


config = AppConfigParser("develop")

RFKL = ['', None, 'None', 'token', 'cms_oid', 'jsonp', '_']
RFVL = ['', None, 'None', 'token', 'cms_oid', 'jsonp', '_']

if __name__ == "__main__":
    print(AppConfigParser().get_postgres_url())
